package com.hopster;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;

public class Month extends Activity{
	
	//data members
	private Calendar display;
	private Calendar  dayCheck; 
	private Calendar mLBound; 
	private Calendar mRBound;

	public int preNonDays, postNonDays; 
	public boolean extra;
	private int monthId, d;
	private List <Event> mEvents;
	
	//Constructor
	public Month()
	{
		display = Calendar.getInstance();
		dayCheck = Calendar.getInstance();
		mLBound = Calendar.getInstance();
		mRBound = Calendar.getInstance();
		mEvents = new ArrayList<Event>();
		setBounds();
		setMonth();
	}

	//Methods
	public void next()
	{//Display next month
		display.add(Calendar.MONTH,1);
		dayCheck.add(Calendar.MONTH,1);
		mLBound.add(Calendar.MONTH,1);
		mRBound.add(Calendar.MONTH,1);
		setMonth();
	}
	
	public void prev()
	{//Display previous month
		display.add(Calendar.MONTH,-1);
		dayCheck.add(Calendar.MONTH,-1);
		mLBound.add(Calendar.MONTH,-1);
		mRBound.add(Calendar.MONTH,-1);
		setMonth();
	}
	
	public String dayFormat(int p)
	{//formats the display of the days

		if ((p > 6 && p < (7 + preNonDays)) || (p > (d + preNonDays + 6)))
		{//display nondays
			p = -1;
		}
		
		//display day of week
		switch(p){
		case(-1):return "";
		case(0): return "Sun";
		case(1): return "Mon";
		case(2): return "Tue";
		case(3): return "Wed";
		case(4): return "Thu";
		case(5): return "Fri";
		case(6): return "Sat";
		
		//display day number
		default: return Epresent(p - (6 + preNonDays));
		}
	}
	
	public void setMonth()
	{//sets the data members of Month for display formatting
		dayCheck.set(Calendar.DAY_OF_MONTH, 1);
		preNonDays = (dayCheck.get(Calendar.DAY_OF_WEEK) - 1);
		monthId = display.get(Calendar.MONTH);
		
		d = display.getActualMaximum(Calendar.DAY_OF_MONTH);
		postNonDays = 49 - (d + preNonDays + 7);
		if (postNonDays >= 7)
		{
			postNonDays = postNonDays - 7;
			extra = false;
		}
		else
		{
			extra = true;
		}
		
		setMEvents();
	}
	
	public String getMonthId()
	{
		switch(monthId)
		{
		case(0):return "January";
		case(1):return "February";
		case(2):return "March";
		case(3):return "April";
		case(4):return "May";
		case(5):return "June";
		case(6):return "July";
		case(7):return "August";
		case(8):return "September";
		case(9):return "October";
		case(10):return "November";
		case(11):return "December";
		default:return"month";
		}
	}
	
	public String getYear()
	{
		return Integer.toString(display.get(Calendar.YEAR));
	}
	
	public Bundle passDate(int pday)
	{
		dayCheck.set(Calendar.DAY_OF_MONTH, pday);
		Bundle mdate = new Bundle();
		mdate.putInt("day", pday);
		mdate.putInt("month", dayCheck.get(Calendar.MONTH));
		mdate.putInt("year", dayCheck.get(Calendar.YEAR));
		mdate.putInt("wkday", dayCheck.get(Calendar.DAY_OF_WEEK));
		return mdate;
	}
	
	public void setBounds()
	{
		mLBound.set(Calendar.DAY_OF_MONTH, 1);
		mLBound.add(Calendar.DAY_OF_MONTH, -1);
		mRBound.add(Calendar.MONTH, 1);
		mRBound.set(Calendar.DAY_OF_MONTH, 1);
	}
	
	public void setMEvents()
	{
		EventService es = EventService.getInstance();
		mEvents.clear();
		List<Event> eList = es.getEvents();
		for(int i = 0; i < eList.size(); i++)
		{
			int L = mLBound.compareTo(eList.get(i).getStartCalendar());
			int R = mRBound.compareTo(eList.get(i).getStartCalendar());
			if(L < 0)
			{
				if(R > 0)
				{
					mEvents.add(eList.get(i));
				}
				else if(R < 0)
				{
					break;
				}
			}
		}
	}
	
	public String Epresent(int d)
	{
		int ev = 0;
		if(!mEvents.isEmpty())
		{
			for(int i = 0; i < mEvents.size(); i++)
			{
				if (d == mEvents.get(i).getStartCalendar().get(Calendar.DAY_OF_MONTH))
				{
					ev++;
				}
			}
			if(ev != 0)
			{
				return Integer.toString(d) + "\nEvents: " + Integer.toString(ev);
			}
		}
		return Integer.toString(d);
	}
	
}
